﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
//using CenterSpace.NMath.Core;
//using CenterSpace.NMath.Stats;
//using System.Linq;

namespace ReadTabDelim
{
    class Program
    {
        static void Main()
        {
            

            //Load the entire file but store each line individually
             //string path = @"C:\code_test\proteinGroups_out.txt";
             string[] lines = File.ReadAllLines("C:\\code_test\\proteinGroups.txt");
             string[] x = lines[0].Split('\t');

             for (int k = 0; k < x.Length; k++)
             
             {
                 for (int i = 0; i < lines.Length; i++)
                 
                 {
                     //Console.WriteLine(lines[i].Split('\t')[k]);
                 }

             }

           
            int[] b = Enumerable.Range(1, 100).ToArray();
            Console.WriteLine(b[4]);
            Console.WriteLine(lines[0].Split('\t')[3]);
            Console.WriteLine(string.Join(" ", lines[0].Split('\t')));
            
            // parse string to double if possible
            double w; 
            if (!double.TryParse("3.4", out w))
            {
                w = double.NaN;
            }


            // generate array of array for strings (list-like)
            string[][] re = new string[5][];
            for (int i = 0; i < 5; i++)
            {
                re[i] = new string[8];
            }

            //generate 2D array (dataframe-like)
            string[,] qg = new string[5,8];

            // write entry to position x,y
            re[3][4] = "hansi";
            qg[3, 4] = "jupp";
            
            
            //read entry from position x,y in qg
            string dd = qg[1, 2];

            string headerLine =lines[0];
            string[] words = headerLine.Split('\t');

            // for searching 1 string match
            int index = IndexOf(words, "Peptide");

            // for searching n string match
            int[] indices = IndicesOf(words, "Peptide");            

            // This text is added only once to the file. 
 
           //Create a file to write to. 
            for (int i = 0; i < lines.Length; i++)
            {
                //string[] createText = {string.Join("\t ", lines[i].Split('\t'))};
                
            }
            string[] createText = { lines[] };
            File.WriteAllLines(@"C:\code_test\proteinGroups_out.txt", createText);
            Console.ReadLine();

            // read file line by line and allows direct manipulation (good for large files as not everything will stay in RAM)
            StreamReader reader = new StreamReader("chansi.txt");
            string line = null;
            List<string> lines3 = new List<string>();
            while ((reader.ReadLine()) != null)
            {
                lines3.Add(line);
            }
        }

        //returns one element of
        public static int IndexOf(string[] array, string element)
        {
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i].Contains(element))
                {
                    return i;
                }
            }
            return -1;
        }



        //returns array of indeces
        public static int[] IndicesOf(string[] array, string element)
        {
            List<int> result = new List<int>();
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i].Equals(element))
                {
                    result.Add(i);
                }
            }
            return result.ToArray();
        }


        //genereic method allowing all variable types (only working for objects that are defined for all variable types(Equals))
        public static int[] IndicesOf<T>(T[] array, T element)
        {
            List<int> result = new List<int>();
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i].Equals(element))
                {
                    result.Add(i);
                }
            }
            return result.ToArray();
        }
    }
}




        // This text is added only once to the file. 
      //  if (!File.Exists(path))
      //  {
            // Create a file to write to. 
       //     string[] createText = { "Hello", "And", "Welcome" };
       //     File.WriteAllLines(path, createText);
       // }